package com.wutongyu.mannyburger;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class RelatedProductAdapter extends RecyclerView.Adapter<RelatedProductAdapter.RelatedProductViewHolder> {

    private Context context;
    private List<Product> relatedProducts;
    private OnItemClickListener listener;

    public RelatedProductAdapter(Context context, List<Product> relatedProducts) {
        this.context = context;
        this.relatedProducts = relatedProducts;
    }

    @NonNull
    @Override
    public RelatedProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_related_product, parent, false);
        return new RelatedProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RelatedProductViewHolder holder, int position) {
        Product product = relatedProducts.get(position);
        
        // 设置商品信息
        holder.productName.setText(product.getName());
        holder.productPrice.setText("¥" + product.getPrice());
        
        // 设置商品图片
        if (product.getImageResId() != 0) {
            holder.productImage.setImageResource(product.getImageResId());
        } else {
            holder.productImage.setImageResource(R.drawable.bg); // 默认图片
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return relatedProducts.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    static class RelatedProductViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        TextView productName;
        TextView productPrice;

        public RelatedProductViewHolder(@NonNull View itemView) {
            super(itemView);
            productImage = itemView.findViewById(R.id.related_product_image);
            productName = itemView.findViewById(R.id.related_product_name);
            productPrice = itemView.findViewById(R.id.related_product_price);
        }
    }
} 